package com.example.libraryapp.webview;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.example.libraryapp.R;
import com.example.libraryapp.util.Validator;

/**
 * WebviewActivity
 * 
 */
public class WebviewActivity extends Activity {
	private TextView tvTitle;
	private WebView myWebView;
	private String title;
	private String url;
	private Context context;
	private TextView tvBack;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.webview_activity);
		this.context = WebviewActivity.this;
		getIntentData();
		findViews();
		setListeners();
		initWebView();
	}

	/**
	 * 设置监听
	 */
	private void setListeners() {
		tvBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				processBack();
			}
		});
	}

	/**
	 * 初始化WebView
	 */
	private void initWebView() {
		// 设置参数
		WebSettings webSettings = myWebView.getSettings();
		webSettings.setDefaultTextEncodingName("utf-8"); // 编码
		webSettings.setJavaScriptEnabled(true); // 设置支持js
		webSettings.setSupportZoom(false); // 不支持缩放
		webSettings.setBuiltInZoomControls(false); // 显示缩放按钮
		webSettings.setGeolocationEnabled(true); // 启用地理定位
		webSettings.setGeolocationDatabasePath(context.getFilesDir().getPath()); // 设置定位的数据库路径
		// 设置自己的Client
		myWebView.setWebViewClient(new MyWebViewClient(context));
		myWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
		myWebView.setHorizontalScrollBarEnabled(true); // 设置水平和垂直的滚动条隐藏
		myWebView.setVerticalScrollBarEnabled(true);
		webSettings.setUseWideViewPort(true); // 可按照任意比例缩放
		webSettings.setLoadWithOverviewMode(true); // 该方法能将网页缩小后全部显示
		float scale = myWebView.getScale();
		myWebView.setInitialScale((int) (100 / scale));

		int screenDensity = getResources().getDisplayMetrics().densityDpi;
		WebSettings.ZoomDensity zoomDensity = WebSettings.ZoomDensity.MEDIUM;
		switch (screenDensity) {
		case DisplayMetrics.DENSITY_LOW:
			zoomDensity = WebSettings.ZoomDensity.CLOSE;
			break;
		case DisplayMetrics.DENSITY_MEDIUM:
			zoomDensity = WebSettings.ZoomDensity.MEDIUM;
			break;
		case DisplayMetrics.DENSITY_HIGH:
			zoomDensity = WebSettings.ZoomDensity.FAR;
			break;
		}
		webSettings.setDefaultZoom(zoomDensity);
	}

	/**
	 * 得到intent传过来的数据
	 */
	private void getIntentData() {
		Intent intent = getIntent();
		title = intent.getStringExtra("title");
		url = intent.getStringExtra("url");
	}

	private void findViews() {
		tvBack = (TextView) this.findViewById(R.id.tv_back);
		tvTitle = (TextView) this.findViewById(R.id.tv_title);
		if (Validator.isEffective(title)) {
			tvTitle.setText(title);
		}
		myWebView = (WebView) this.findViewById(R.id.myWebView);
		if (Validator.isEffective(url)) {
			myWebView.loadUrl(url);
		}
	}

	/**
	 * 自定义的WebViewClient
	 * 
	 * @author josan_tang
	 * 
	 */
	public static class MyWebViewClient extends WebViewClient {
		private Context context;

		public MyWebViewClient(Context context) {
			this.context = context;
		}

		/**
		 * 请求完成时回调的方法
		 * 
		 * @param view
		 * @param url
		 */
		@Override
		public void onPageFinished(WebView view, String url) {
			super.onPageFinished(view, url);
		}

		/**
		 * 请求开始时回调的方法
		 * 
		 * @param view
		 * @param url
		 * @param favicon
		 */
		@Override
		public void onPageStarted(WebView view, String url, Bitmap favicon) {
			// TODO Auto-generated method stub
			super.onPageStarted(view, url, favicon);
		}

		@Override
		public void onReceivedError(WebView view, int errorCode,
				String description, String failingUrl) {
			// TODO Auto-generated method stub
			super.onReceivedError(view, errorCode, description, failingUrl);
		}

		/**
		 * 当Api小于11时，并不是每次都会调用
		 */
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			view.loadUrl(url);
			return true;
		}
	}

	/**
	 * 处理回退事件
	 */
	private void processBack() {
		if (myWebView.canGoBack()) {
			myWebView.goBack();
		} else {
			this.finish();
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			processBack();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

}
